package cn.yinyu.queue.module.ecg.dal.mysql.devmanage;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.yinyu.queue.framework.mybatis.core.mapper.BaseMapperX;
import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DeviceDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DeviceStatisticDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordStatisticDO;
import org.apache.ibatis.annotations.*;
import cn.yinyu.queue.module.ecg.controller.admin.devmanage.vo.*;

/**
 * 设备 Mapper
 *
 * @author majianbo
 */
@Mapper
public interface DeviceMapper extends BaseMapperX<DeviceDO> {

    default PageResult<DeviceDO> selectPage(DevicePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DeviceDO>()
                .eqIfPresent(DeviceDO::getDevId, reqVO.getDevId())
                .eqIfPresent(DeviceDO::getDevCodeIntrinsic, reqVO.getDevCodeIntrinsic())
                .eqIfPresent(DeviceDO::getDevCodeHosp, reqVO.getDevCodeHosp())
                .eqIfPresent(DeviceDO::getDevCodeDept, reqVO.getDevCodeDept())
                .inIfPresent(DeviceDO::getState, reqVO.getState())
                .eqIfPresent(DeviceDO::getCategory, reqVO.getCategory())
                .eqIfPresent(DeviceDO::getBrand, reqVO.getBrand())
                .eqIfPresent(DeviceDO::getModel, reqVO.getModel())
                .betweenIfPresent(DeviceDO::getPurchaseDate, reqVO.getPurchaseDate())
                .betweenIfPresent(DeviceDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(DeviceDO::getDevId));
    }

    @Select("select * from yy_queue.device where dev_id=#{devId}")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    DeviceDO getDeviceByDevId(@Param("devId") String devId);

    @Update("update yy_queue.device set state=#{state}, state_date=#{stateDate}, comment=#{comment} where dev_id=#{devId}")
    Integer updateDevState(@Param("devId") String devId, @Param("state") Integer state, @Param("stateDate") LocalDate stateDate, @Param("comment") String comment);

    @Select("<script>" +
            "select category, state, count(1) as dev_count from yy_queue.device " +
            " <where> deleted = 0 " +
            "   <if test=\"category != null and category != ''\"> " +
            "     and category = #{category} " +
            "   </if> " +
            "   <if test=\"brand != null and brand != ''\"> " +
            "     and brand = #{brand} " +
            "   </if> " +
            "   <if test=\"model != null and model != ''\"> " +
            "     and model = #{model} " +
            "   </if> " +
            " </where> " +
            " group by category, state " +
            " order by category " +
            "</script>")
    List<DeviceStatisticDO> getDevStatistic(DevicePageReqVO pageReqVO);

}
